package com.group3.merchant.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group3.entity.Dish;
import com.group3.entity.DishType;
import com.group3.entity.Merchant;
import com.group3.merchant.service.IDishService;
import com.group3.merchant.service.IDishTypeService;
import com.group3.merchant.service.impl.DishServiceImpl;
import com.group3.merchant.service.impl.DishTypeServiceImpl;
import com.group3.merchant.utils.SenderUtil;

/**
 * Servlet implementation class ListDishServlet
 */
public class ListDishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IDishService dishService = new DishServiceImpl();
	private IDishTypeService dishTypeService = new DishTypeServiceImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ListDishServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		listDish(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		listDish(request, response);
	}

	private void listDish(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	    Map<String,Object> map = new HashMap<String,Object>();
		String dType=request.getParameter("dType");
		Merchant merchant=((Merchant)request.getSession().getAttribute("loginToken"));
		System.out.println("Merchant: " + merchant);
		String mId = merchant.getmId();
		List<Dish> dishes=new ArrayList<Dish>();
		System.out.println("DB dType:"+dType);
		
		//对dType做个判断
		if("000".equals(dType)||dType == null){
			dishes = dishService.showByMId(mId);
		}else{
			dishes = dishService.showByMTid(mId, dType);
		}
		List<DishType> dishtype = dishTypeService.showAllType();
		
		map.put("dishes", dishes);
		map.put("merchant", merchant);
		map.put("dishtype", dishtype);
		
		SenderUtil.send(map, response);
		
	}
}
